﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.UI.BaseDataSvc;

namespace YY.UI
{
    public partial class ClientFrm : BaseFrm
    {
        /// <summary>
        /// 数据表临时变量
        /// </summary>
        DataTable _DT = null;

        /// <summary>
        /// 窗体是否选择模式
        /// </summary>
        public bool SearchMode { get; set; }

        /// <summary>
        /// 窗体选择模式时选中的返回
        /// </summary>
        public TB_Clients SelectorItem { get; set; }

        public ClientFrm()
        {
            InitializeComponent();
            YY.Common.EventCommon.ClientUdDGVEvnt += new YY.Common.EventCommon.UpdateDataGridViewDlgt(updateDgv);
            this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();            
        }

        /// <summary>
        /// 初始化表格列
        /// </summary>
        private void initDgvColumns()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;//AutoSize十分影响性能
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "id", "客户Id", false, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "fullName", "客户全称", true, true, 180);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "shortName", "客户简称", true, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "contactMan", "联系人", true, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "tel", "联系电话", true, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "fax", "传真号码", true, true, null);
            Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "address", "联系地址", true, true, 200);
        }

        #region 委托回调
        private void updateDgv(object o, EventArgs arg)
        {
            if (arg != null)
            {
                CommonEventArg<TB_Clients> argObj = (CommonEventArg<TB_Clients>)arg;
                if (argObj.OpType == Common.OperateType.Add)
                {
                    //if (this._DT != null && this._DT.Rows.Count > 0)
                    //{
                    //    DataRow dr = this._DT.NewRow();
                    //    OTConverter.ObjectPropertyToDataRow<TB_Clients>(argObj.t, this._DT.Columns, dr);
                    //    this._DT.Rows.Add(dr);
                    //}
                    //else
                    //{
                    //    TB_Clients[] ary = new TB_Clients[] { argObj.t };
                    //    this._DT = ary.ConvertToDataTable<TB_Clients>();
                    //}
                    search();
                }
                else if (argObj.OpType == Common.OperateType.Update)
                {
                    search();
                    //DataRow[] drs = this._DT.Select("id=" + argObj.t.id);
                    //if (drs != null)
                    //{
                    //    foreach (DataRow dr in drs)
                    //        OTConverter.ObjectPropertyToDataRow<TB_Clients>(argObj.t, this._DT.Columns, dr);
                    //}
                }
                else if (argObj.OpType == Common.OperateType.Delete)
                    deleteRow(false, argObj.t.id);
            }
        }
        #endregion

        private void ClientFrm_Load(object sender, EventArgs e)
        {
            if (this.SearchMode)
                this.tsBtnConfirm.Visible = true;
            else
                this.tsBtnConfirm.Visible = false;
            initDgvColumns();
            this.BaseDataProxy.Open();
            search();
        }

        private void search()
        {
            TB_Clients[] datas = this.BaseDataProxy.FindClients(null, this.txtBoxName.Text, this.txtBoxTel.Text, this.txtBoxAddress.Text, this.txtBoxContactMan.Text);
            this._DT = datas.ToDataTable();
            this.dataGridView1.DataSource = this._DT;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            search();
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            EditClientFrm frm = new EditClientFrm();
            frm.ShowDialog(this);
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
                deleteRow(true, Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value));
        }

        /// <summary>
        /// 数据源删除选中行
        /// </summary>
        private void deleteRow(bool serverDelete, int id)
        {
            if (serverDelete)
            {
                if (MessageBox.Show("确认删除该项吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (!this.BaseDataProxy.DeleteClient(id))
                    {
                        MessageBox.Show("删除异常！");
                        return;
                    }
                }
                else
                    return;

            }
            DataRow[] drs = this._DT.Select("id=" + id.ToString());
            foreach (DataRow dr in drs)
                this._DT.Rows.Remove(dr);
            this._DT.AcceptChanges();
        }


        private void tsBtnConfirm_Click(object sender, EventArgs e)
        {
            if (this.SearchMode)
                returnSelectItem();
        }

        private void returnSelectItem()
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                this.SelectorItem = OTConverter.ConvertToObject<TB_Clients>(this._DT.Rows[this.dataGridView1.CurrentRow.Index]);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.SearchMode)
                returnSelectItem();
            else
            {
                if (this.dataGridView1.CurrentRow != null)
                {
                    EditClientFrm frm = new EditClientFrm();                    
                    frm.EditMode = true;
                    frm.ID = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value);
                    frm.ShowDialog(this);
                }
            }
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            Common.ShowDataGridViewRowNo(sender);
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            Common.ShowDataGridViewRowNo(sender);
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            YY.Common.EventCommon.ClientUdDGVEvnt -= new YY.Common.EventCommon.UpdateDataGridViewDlgt(updateDgv);
        }
    }
}
